package com.crb.smartcard.cardutils;


import com.crb.smartcard.SmartCardConfig;
import com.crb.smartcard.cardutils.entity.SetDefaultCardEntity;
import com.crb.smartcard.core.bean.CardResult;
import com.crb.smartcard.core.oma.SmartCard;
import com.crb.smartcard.utils.LogUtil;
import com.crb.smartcard.utils.StringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 设置默认卡
 */
public class SetDefaultCardUtil {

    private static final String TAG = "SetDefaultCardUtil";

    /**
     * 1、与CRS建立通道（AID：A00000015143525300）
     * 2、发送指令激活/去激活指令：
     * 80F001+P2(1字节，01：激活;00：去激活)++LC(1字节，数据域长度)+4F+AID长度（1字节）+AID
     */
    public static SetDefaultCardEntity setDefaultCard(String aid1, String aid2) {
        //01：激活;00：去激活
        if (StringUtil.isEmpty(aid1)) {
            //只设置aid2位默认卡
            List<String> cApduList = new ArrayList<>();
            List<CardResult> cardResultList = new ArrayList<>();
            cApduList.add("00A4040009A00000015143525300");
            String aidLC = StringUtil.getStringLc(aid2);
            String stringLc = StringUtil.getStringLc("4F" + aidLC + aid2);
            cApduList.add("80F001" + "01" + stringLc + "4F" + aidLC + aid2);
//            cApduList.add("80F240000F4F08A0000006320101055C039F704F00");
            for (String string : cApduList) {
                LogUtil.e(TAG, string);
                CardResult execute = SmartCard.execute(string);
                LogUtil.e(TAG, execute.toString());
                cardResultList.add(execute);
            }
            SetDefaultCardEntity setDefaultCardEntity = new SetDefaultCardEntity();
            LogUtil.e(TAG, cardResultList.get(1).toString());
            if (cardResultList.get(1).getStatus() == 0 && cardResultList.get(1).getSw().equals("9000")) {
                setDefaultCardEntity.setCode(SmartCardConfig.SUCCESSFUL_CODE);
                setDefaultCardEntity.setInfo("设置默认卡成功");
            } else {
                setDefaultCardEntity.setCode(SmartCardConfig.FAILURE_CODE);
                setDefaultCardEntity.setInfo("设置默认卡失败");
            }
            SmartCard.closeChannel();
//            LogUtil.e(TAG, new Gson().toJson(setDefaultCardEntity));
            return  setDefaultCardEntity;
        } else {
            //将aid1设置为非默认卡，aid2设为默认卡
            List<String> cApduList = new ArrayList<>();
            List<CardResult> cardResultList = new ArrayList<>();
            cApduList.add("00A4040009A00000015143525300");
            String aidLC1 = StringUtil.getStringLc(aid1);
            String stringLc1 = StringUtil.getStringLc("4F" + aidLC1 + aid1);
            cApduList.add("80F001" + "00" + stringLc1 + "4F" + aidLC1 + aid1);
            String aidLC2 = StringUtil.getStringLc(aid2);
            String stringLc2 = StringUtil.getStringLc("4F" + aidLC2 + aid2);
            cApduList.add("80F001" + "01" + stringLc2 + "4F" + aidLC2 + aid2);
//            cApduList.add("80F240000F4F08A0000006320101055C039F704F00");
            for (String string : cApduList) {
                LogUtil.e(TAG + "------1", string);
                CardResult execute = SmartCard.execute(string);
                LogUtil.e(TAG + "------2", execute.toString());
                cardResultList.add(execute);
            }

            SmartCard.closeChannel();
            if(cardResultList.get(2).getSw().equals("9000")){
                SetDefaultCardEntity setDefaultCardEntity = new SetDefaultCardEntity();
                setDefaultCardEntity.setCode(SmartCardConfig.SUCCESSFUL_CODE);
                setDefaultCardEntity.setInfo("设置默认卡成功");
//                LogUtil.e(TAG + "------8", new Gson().toJson(setDefaultCardEntity));
//                return new Gson().toJson(setDefaultCardEntity);
                return setDefaultCardEntity;
            }else {
                SetDefaultCardEntity setDefaultCardEntity = new SetDefaultCardEntity();
                setDefaultCardEntity.setCode(SmartCardConfig.FAILURE_CODE);
                setDefaultCardEntity.setInfo("设置默认卡失败");
//                LogUtil.e(TAG + "------8", new Gson().toJson(setDefaultCardEntity));
//                return new Gson().toJson(setDefaultCardEntity);
                return setDefaultCardEntity;
            }


           /* for (String string : cApduList) {
                LogUtil.e(TAG + "------1", string);
                CardResult execute = SmartCard.execute(string);
                LogUtil.e(TAG + "------2", execute.toString());
                cardResultList.add(execute);
            }
            LogUtil.e(TAG + "==========get(1)", cardResultList.get(1).toString());
            for (CardResult cardResult : cardResultList) {
                LogUtil.e(TAG + "==========", cardResult.toString());
            }
            if (cardResultList.get(1).getSw().equals("6330")) {
                CardResult checkDefaultCardString = SmartCard.execute("80F240000F4F08A0000006320101055C039F704F00");
                String[] split = checkDefaultCardString.getRapdu().split("61");
                for (String str : split) {
                    if (str.contains(aid1)) {
                        if (str.substring(10, 12).equals("00")) {
                            //反激活成功-》去激活第二张
                            String aidLC2 = StringUtil.getStringLc(aid2);
                            String stringLc2 = StringUtil.getStringLc("4F" + aidLC2 + aid2);
                            CardResult execute = SmartCard.execute("80F001" + "01" + stringLc2 + "4F" + aidLC2 + aid2);
                            if (execute.getSw().equals("9000")) {
                                //第二张激活成功
                                SetDefaultCardEntity setDefaultCardEntity = new SetDefaultCardEntity();
                                setDefaultCardEntity.setCode(JSCardConfig.SUCCESSFUL_CODE);
                                setDefaultCardEntity.setInfo("设置默认卡成功");
                                LogUtil.e(TAG + "------3", new Gson().toJson(setDefaultCardEntity));
                                return new Gson().toJson(setDefaultCardEntity);
                            } else {
                                //第二张激活失败
                                SetDefaultCardEntity setDefaultCardEntity = new SetDefaultCardEntity();
                                setDefaultCardEntity.setCode(JSCardConfig.FAILURE_CODE);
                                setDefaultCardEntity.setInfo("设置默认卡失败");
                                LogUtil.e(TAG + "------4", new Gson().toJson(setDefaultCardEntity));
                                return new Gson().toJson(setDefaultCardEntity);
                            }
                        } else {
                            //反激活失败
                            SetDefaultCardEntity setDefaultCardEntity = new SetDefaultCardEntity();
                            setDefaultCardEntity.setCode(JSCardConfig.FAILURE_CODE);
                            setDefaultCardEntity.setInfo("设置默认卡失败");
                            LogUtil.e(TAG + "------5", new Gson().toJson(setDefaultCardEntity));
                            return new Gson().toJson(setDefaultCardEntity);
                        }
                    }
                }
            } else {
                String aidLC2 = StringUtil.getStringLc(aid2);
                String stringLc2 = StringUtil.getStringLc("4F" + aidLC2 + aid2);
                CardResult execute = SmartCard.execute("80F001" + "01" + stringLc2 + "4F" + aidLC2 + aid2);
                if (execute.getSw().equals("9000")) {
                    //第二张激活成功
                    SetDefaultCardEntity setDefaultCardEntity = new SetDefaultCardEntity();
                    setDefaultCardEntity.setCode(JSCardConfig.SUCCESSFUL_CODE);
                    setDefaultCardEntity.setInfo("设置默认卡成功");
                    LogUtil.e(TAG + "------6", new Gson().toJson(setDefaultCardEntity));
                    return new Gson().toJson(setDefaultCardEntity);
                } else {
                    //第二张激活失败
                    SetDefaultCardEntity setDefaultCardEntity = new SetDefaultCardEntity();
                    setDefaultCardEntity.setCode(JSCardConfig.FAILURE_CODE);
                    setDefaultCardEntity.setInfo("设置默认卡失败");
                    LogUtil.e(TAG + "------7", new Gson().toJson(setDefaultCardEntity));
                    return new Gson().toJson(setDefaultCardEntity);
                }
            }*/
        }
      /*  SetDefaultCardEntity setDefaultCardEntity = new SetDefaultCardEntity();
        setDefaultCardEntity.setCode(JSCardConfig.FAILURE_CODE);
        setDefaultCardEntity.setInfo("设置默认卡失败");
        LogUtil.e(TAG + "------8", new Gson().toJson(setDefaultCardEntity));
        return new Gson().toJson(setDefaultCardEntity);*/
    }
}
